﻿@page "/UserUpdate/{Id:int?}"
@using Carlife.Inf;
@using ICarlifeService;
@using Models;
@using Microsoft.EntityFrameworkCore;
@using Models.Authority;
@inject IDbContextFactory<CarlifeDbcontext> dbFactory;
@inject IUsersService userinfo;
@inject IJSRuntime jsRuntime
@inject NavigationManager NavigationManager
<PageTitle>用户修改</PageTitle>
<h3>@UpdText</h3>
<EditForm Model="@users" OnSubmit="@HandleSubmit">
    <div>@Message</div>
    <div>
        <p>
            登录帐号 ：
            <input @bind="@users.Login_Name" type="text"></input>
        </p>
        <p>
            用户名称：
            <input @bind="@users.UserName" type="text"></input>
        </p>
        <p>
            密码：
            <input @bind="@users.Password" type="password"></input>
        </p>
        <p>
            真实姓名：
            <input @bind="@users.RealName" type="text"></input>
        </p>
        <p>
            英文名称：
            <input @bind="@users.EnglishName" type="text"></input>
        </p>
        <p>
            性别：
            <input type="radio" @bind="@users.Gender" name="sex">男
            <input type="radio" @bind="@users.Gender" name="sex">女
        </p>
        <p>
            国际电话：
            <input @bind="@users.Phone" type="text"></input>
        </p>
        <p>
            移动电话：
            <input @bind="@users.Mobile" type="text"></input>
        </p>
        <p>
            邮箱：
            <input @bind="@users.Email" type="text"></input>
        </p>
        <p>
            启用禁用：
            <input type="radio" @bind="@users.Enabled" name="qy">启用
            <input type="radio" @bind="@users.Enabled" name="qy">禁用
        </p>
        <p>
            类型：
            <input type="radio" @bind="@users.Type" name="yx">允许
            <input type="radio" @bind="@users.Type" name="yx">不允许
        </p>
        <button type="submit">@UpdText</button>
    </div>
</EditForm>
@code {
    private string UpdText = "修改";
    private string res = "";
    [Parameter]
    public int? Id { get; set; }
    private string Message = string.Empty;
    //private static CarlifeDbcontext _context;
    private Users users = new Users();
    protected override void OnInitialized()
    {
        //_context = dbFactory.CreateDbContext();
        base.OnInitialized();
        if (Id.HasValue)
        {
            users = userinfo.GetUserByid(Id.Value);
            UpdText = "编辑";
        }
    }
    private async void HandleSubmit()
    {
        users.IsData = true;
        if (Id.HasValue)
        {
            userinfo.GetUserUpdate(users);
            await jsRuntime.InvokeAsync<string>("alert", "编辑成功 !");
            //Message = "编辑成功 !";
            NavigationManager.NavigateTo($"UserIndex");
        }
        else
        {
            await jsRuntime.InvokeAsync<string>("alert", "编辑失败 !");
            //Message = "编辑失败 !";
        }
    }
}
